package com.syezon.wifikey.utils;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import org.json.JSONObject;

/**
 * Created by Administrator on 2019/1/11.
 */

public class ClientInfoUtils {

    public static final String DEFAULT_INFO = "unknown";

    public static String OAID = "unknown";

    public static String getImei() {
        String imei = "unknown";
        try {

        } catch (Exception e) {

        }
        String deviceId = "";
        try {
            TelephonyManager tm = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null && tm.getDeviceId() != null) {
                deviceId = tm.getDeviceId();
            }
        } catch (Exception e) {
        }
        if (TextUtils.isEmpty(deviceId)) {
            try {
                TelephonyManager tm = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null && tm.getImei() != null) {
                    imei = tm.getImei();
                }
                if (TextUtils.isEmpty(imei)) {
                    imei = "unknown";
                }
            } catch (Exception e) {
            }
        } else {
            imei = deviceId;
        }
        if ("unknow".equals(imei)) {
            imei = OAID;
        }
        return imei;

    }

    public static String getOAID() {
        return OAID;
    }

    public static String getGetway() {
        String getway = DEFAULT_INFO;
        try {
            WifiManager wifi = (WifiManager) Utils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            DhcpInfo di = wifi.getDhcpInfo();
            if (di != null) {
                long getewayIpL = di.gateway;
                getway = long2ip(getewayIpL);//网关地址
            }
        } catch (Exception e) {
            getway = DEFAULT_INFO;
        }
        return getway;
    }

    private static String long2ip(long ip) {
        StringBuffer sb = new StringBuffer();
        sb.append(String.valueOf((int) (ip & 0xff)));
        sb.append('.');
        sb.append(String.valueOf((int) ((ip >> 8) & 0xff)));
        sb.append('.');
        sb.append(String.valueOf((int) ((ip >> 16) & 0xff)));
        sb.append('.');
        sb.append(String.valueOf((int) ((ip >> 24) & 0xff)));
        return sb.toString();
    }

    public static String getWifiMac() {
        WifiManager wifi = (WifiManager) Utils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String str = info.getMacAddress();
        if (str == null) str = DEFAULT_INFO;
        return str;
    }

    public static String getBrandModel() {
        String result = DEFAULT_INFO;
        try {
            JSONObject brandModelJson = new JSONObject();
            brandModelJson.put("brand", Build.BRAND);
            brandModelJson.put("model", Build.MODEL);
            result = brandModelJson.toString();
        } catch (Exception e) {

        }
        return result;
    }

}
